//
//  SignInViewController.m
//  Courses
//
//  Created by xxx on 15/1/8.
//  Copyright (c) 2015年 wtf. All rights reserved.
//

#import "SignInViewController.h"

#import "SignUpViewController.h"

#import "User.h"
#import "MobClick.h"
@implementation SignInViewController
{
    UITextField *_txtUsername;
    UITextField *_txtPassword;
}

@synthesize s_delegate=_s_delegate;

SYNTHESIZE_SINGLETON_FOR_IMPL(SignInViewController)

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [WTFColors backgroundColorLightGreen];
    
    UIButton *btnDismiss = [[UIButton alloc] initWithFrame:AutoRectMake(0, 0, 66, 44, WTFAutoLayoutTypeRaw)];
    btnDismiss.titleLabel.textAlignment = NSTextAlignmentLeft;
    [btnDismiss setTitle:@"取消" forState:UIControlStateNormal];
    [btnDismiss setTintColor:[UIColor whiteColor]];
    [btnDismiss addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnDismiss];
    
    self.title = @"登录";
    
    UIButton *signUpButton = [[UIButton alloc] initWithFrame:AutoRectMake(0, 0, 44, 44, WTFAutoLayoutTypeRaw)];
    [signUpButton setTitle:@"注册" forState:UIControlStateNormal];
    [signUpButton addTarget:self action:@selector(showSignUpViewController) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *homeButtonItem = [[UIBarButtonItem alloc]initWithCustomView:signUpButton];
    self.navigationItem.rightBarButtonItem = homeButtonItem;
    
    UILabel *indicatorUsername = [[UILabel alloc] initWithFrame:AutoRectMake(20, 20, 90, 40, WTFAutoLayoutTypeBar)];
    indicatorUsername.text = @"  手机号码";
    indicatorUsername.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:indicatorUsername];
    
    UILabel *indicatorPassword = [[UILabel alloc] initWithFrame:AutoRectMake(20, 70, 90, 40, WTFAutoLayoutTypeBar)];
    indicatorPassword.text = @"  密码";
    indicatorPassword.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:indicatorPassword];
    
    _txtUsername = [[UITextField alloc] initWithFrame:AutoRectMake(100, 20, 200, 40, WTFAutoLayoutTypeBar)];
    _txtUsername.placeholder = @"手机号码";
    _txtUsername.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_txtUsername];
    _txtPassword = [[UITextField alloc] initWithFrame:AutoRectMake(100, 70, 200, 40, WTFAutoLayoutTypeBar)];
    _txtPassword.placeholder = @"6-20位数字、字母和字符";
    _txtPassword.backgroundColor = [UIColor whiteColor];
    _txtPassword.secureTextEntry = YES;
    [self.view addSubview:_txtPassword];
    
    UIButton *loginButton = [[UIButton alloc] initWithFrame:AutoRectMake(20, 150, DEFAULT_WIDTH - 40, 44, WTFAutoLayoutTypeBar)];
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    loginButton.backgroundColor = [WTFColors defaultIconColor];
    [loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
}

- (void)login
{
    NSLog(@"do login with username: %@, password: %@", _txtUsername.text, _txtPassword.text);
  
//    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // sign in
        BOOL signedIn = [User signInWithMobile:_txtUsername.text password:_txtPassword.text];
        dispatch_async(dispatch_get_main_queue(), ^{
            // dismiss
            if (signedIn)
            {
                if ([self.s_delegate  respondsToSelector:@selector(changeStatus)]) {
                    [self.s_delegate changeStatus];
                }
                [self.navigationController dismissViewControllerAnimated:YES completion:nil];
            }
            else
            {
                _txtPassword.text = @"";
                NSLog(@"账号密码错误");
                UIAlertView* errorView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"账号或密码错误!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:NULL, nil];
                [errorView show];
            }
        });
    });
}

- (void)showSignUpViewController
{
    NSLog(@"show sign up vc");
    //统计进入注册的 页面
    [MobClick endEvent:@"RegisterCount"];
    [self.navigationController pushViewController:[SignUpViewController sharedInstance] animated:YES];
}

- (void)dismiss
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

+ (void)popUpOnVc:(UIViewController *)vc
{
    //
    SignInViewController *signInVc = [[SignInViewController alloc] init];
    signInVc.s_delegate=(id)vc;
    UINavigationController *navCtrlr = [[UINavigationController alloc] initWithRootViewController:signInVc];
    navCtrlr.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
    [vc.navigationController presentViewController:navCtrlr animated:YES completion:nil];
}

@end
